package org.grottarossa.ui.core.container;

import java.util.ArrayList;

import org.grottarossa.template.data.enums.EnumDataType;
import org.grottarossa.template.data.pojo.table.Cell;
import org.grottarossa.template.data.pojo.table.Record;
import org.grottarossa.template.data.pojo.table.Table;
import org.grottarossa.template.data.pojo.users.User;
import org.grottarossa.template.data.pojo.users.UserList;

import com.vaadin.data.util.IndexedContainer;

public class UserListContainer extends TableContainer
{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	public static String LOGINID ="Nome utente";
	public static String DISPLAY_NAME ="Nome visualizzato";
	public static String ROLES ="Ruoli";

	public UserListContainer(UserList users)
	{
		super();

		Table data = new Table();
		data.getHeader().add(new Cell(EnumDataType.String, LOGINID));
		data.getHeader().add(new Cell(EnumDataType.String, DISPLAY_NAME));
		data.getHeader().add(new Cell(EnumDataType.String, ROLES));

		for (User user: users)
		{
			Record r = new Record();
			r.add(new Cell(EnumDataType.String, user.getLoginID()));
			r.add(new Cell(EnumDataType.String, user.getDisplayName()));
			r.add(new Cell(EnumDataType.String, formatRoles(user.getRoles())));
			data.getRecords().add(r);
		}

		setDataTable(data);
	}

	private String formatRoles(ArrayList<String> roles)
	{
		String formattedRoles = "";

		for(String role : roles)
		{
			if(role != null && !role.trim().equals(""))
				formattedRoles += role + ", ";
		}

		if(formattedRoles.endsWith(", "))
			formattedRoles = formattedRoles.substring(0, formattedRoles.length()-2);

		return formattedRoles;
	}
}
